package edu.univercidade.tcc.server.service;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;

import edu.univercidade.tcc.server.model.Tracker;
import edu.univercidade.tcc.server.model.Workflow;

public class WorkflowServiceImpl implements WorkflowService {

	@PersistenceContext
	private transient EntityManager entityManager;

	public boolean hasDefaultAndFinalStatus(Tracker tracker) {
		return entityManager
				.createQuery(
						"SELECT w FROM Workflow AS w "
								+ "WHERE w.oldIssueStatus.isDefault = TRUE "
								+ "AND w.tracker = :tracker", Workflow.class)
				.setParameter("tracker", tracker).getResultList().size() > 0
				&& entityManager
						.createQuery(
								"SELECT w FROM Workflow AS w "
										+ "WHERE w.newIssueStatus.isClosed = TRUE "
										+ "AND w.tracker = :tracker",
								Workflow.class)
						.setParameter("tracker", tracker).getResultList()
						.size() > 0;
	}
}
